
var token, url = getApp().apiUrl("/product/list"), page = 1, id = "", index = "rank", type = "asc", content = "", objectId = "", brand_id = "", filter_attr = "", label_id = "";
var imgUrl = getApp().globalData.imgUrl;
let NORMAL = 0, LOWER = 1, PULLDOWN = 2
let app = getApp()
let tapIndex = 0 // 排序的index
    
var fnc = function(that, flag) {
  if (flag == LOWER) {
    that.setData({
      loading: true,
    })
  } 
  that.setData({
      hidden: !1
  }), token= wx.getStorageSync("token"), 1 == that.data.isListData && wx.request({
    url: url,
    data: {
      name: content,
      type: type,
      sort: index,
      filter_attr: filter_attr,
      page: that.data.offset,
      page_size: that.data.limit,
      label_id: label_id
    },
    method: "GET",
    header: {
      "Authorization": 'Bearer ' + token
    },
    success: function(res) {
      console.log(res.data.data)
      let listTemp = []
      let loadingComplete = false
      if (res.data.code > app.globalData.successCode) {

        if (res.data.code != 19001) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else {
          loadingComplete = true
        }

        that.setData({
          list: [],
          loaded: true,
        })

      } else if (res.data.code == app.globalData.successCode) {
        app.login(that, 0)
      } else {
        if (!(res.data.data.length == that.data.limit)) {
          loadingComplete = true
        }
        listTemp = res.data.data

        for (var i = 0; i < listTemp.length; i++) {

          listTemp[i].pic_url = app.globalData.imgUrl + listTemp[i].file.savepath + listTemp[i].file.savename
          listTemp[i].price = listTemp[i].price.toFixed(2)
          listTemp[i].old_price = listTemp[i].old_price.toFixed(2)
        }


        that.setData({
          list: flag == LOWER ? that.data.list.concat(listTemp) : listTemp,
          loadingComplete: loadingComplete
        })

      }  
    }, fail: function (res) {
      that.setData({
        noConnect: true
      })
    },
    complete: function (res) {
      that.setData({
        loaded: true
      })
      if (flag == LOWER) {
        that.setData({
          loading: false,
        })
      } else if (flag == PULLDOWN) {
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }
  });
};

Page({
    data: {
      current: "0",
      list: [],
      scrollTop: 0,
      showView: true,
      arrange: "arrange",
      isListData: !0,
      hiddenCateAll: !1,
      hiddenNum: !0,
      hiddenPrice: !0,
      hiddenSynthesize: !0,
      showTop: !1,
      showBot: !0,
      imgUrl:imgUrl,
      offset: 1,
      limit: 10,
      loading: false,
      loadingComplete: false,
      loaded: false,
      noConnect: false,
    },
    onLoad: function(options) {
        
      var that = this;
      if (options) {
        if (options.label_id) {
          // console.log('label_ID')
          // console.log('label_ID')
          label_id = options.label_id
          fnc(that, NORMAL)
        } else {
          objectId = options.objectId ? options.objectId : "", id = options.id ? options.id : "", content = options.content ? options.content : "",
            brand_id = options.brand_id ? options.brand_id : "", filter_attr = options.filter_attr ? options.filter_attr : "", that.setData({
              category: options.id ? options.id : ""
            }), fnc(that, NORMAL);
        }
        
      }
       
    },
    
    //去筛选页面
    toSynthesize: function() {
        var str =``;
        if(id){
            str+="?objectId="+id
        }

        if(content){
            str += str?"&content="+content:"?content="+content;
        }
        wx.redirectTo({
            url: "../category/screen"+str
        });
    },
    toCateAllTap: function () {
      this.toCateAll(NORMAL)
    },
    toCateAll: function(flag) {
        tapIndex = 0
        var that= this

        if (flag == NORMAL)
          that.setData({
            hiddenCateAll: !1,
            hiddenSynthesize: !0,
            hiddenNum: !0,
            hiddenPrice: !0,
            list: [],
            isListData: !0,
            scrollTop: 0,
            viewBox: !1,
            offset: 1,
            limit: 10,
            loading: false,
            loadingComplete: false,
            loaded: false,
            noConnect: false
          })
        index = "rank"
        fnc(that, flag)
    },
    toNumTap: function () {
      this.toNum(NORMAL)
    },
    toNum: function(flag) {
        tapIndex = 1
        var that = this
        if (flag == NORMAL)
          that.setData({
            hiddenCateAll: !0,
            hiddenSynthesize: !0,
            hiddenNum: !1,
            hiddenPrice: !0,
            list: [],
            isListData: !0,
            scrollTop: 0,
            viewBox: !1,
            offset: 1,
            limit: 10,
            loading: false,
            loadingComplete: false,
            loaded: false,
            noConnect: false
          })
        index = "sale"
        fnc(that, flag)
    },
    toPriceTap: function () {
      this.toPrice(NORMAL)
    },
    toPrice: function(flag) {
        tapIndex = 2
        var that = this
        type = "asc" == type ? "desc" : "asc"
        if (flag == NORMAL)
          that.setData({
            hiddenCateAll: !0,
            hiddenSynthesize: !0,
            hiddenNum: !0,
            hiddenPrice: !1,
            showPrice: !that.data.showPrice,
            showTop: !that.data.showTop,
            showBot: !that.data.showBot,
            list: flag != LOWER? []:that.data.list,
            isListData: !0,
            scrollTop: 0,
            viewBox: !1,
            offset: 1,
            limit: 10,
            loading: false,
            loadingComplete: false,
            loaded: false,
            noConnect: false
          })
        index = "price"
        fnc(that, flag);
    },
    onChangeShowState: function() {
        var that= this;
        that.setData({
            showView: !that.data.showView,
            arrange: that.data.arrange ? "" : "arrange"
        });
    },

  /**
   * 跳转至搜索页
   */
  toSearch: function () {
    wx.navigateTo({
      url: "/pages/search/index?fromID=1"
    });  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("toLower" + this.data.loadingComplete)
    this.setData({
      loading: this.data.loading,
      loadingComplete: this.data.loadingComplete
    })
    if (!this.data.loadingComplete) {
      this.data.offset += 1
      if (tapIndex == 0)
        this.toCateAll(LOWER)
      else if (tapIndex == 1)
        this.toNum(LOWER)
      else 
        this.toPrice(LOWER)
    }
  }
});